<?php
require('../config.php');
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_error) {    
    die("连接失败: " . $conn->connect_error);    
}  
$response = [];
$user_name = $_GET['account'];
$pass_word = $_GET['password'];
$mail = $_GET['mail'];
if (empty($user_name) || empty($pass_word) || empty($mail)) {
    die("缺少必要的参数");
}


function generateUniqueId($conn, $length = 7) {
    $characters = '0123456789';
    $id = '';
    do {
        $id = '';
        for ($i = 0; $i < $length; $i++) {
            $id .= $characters[rand(0, strlen($characters) - 1)];
        }
        $result = $conn->query("SELECT 1 FROM users WHERE id = '$id'");
        if ($result !== false && $result->num_rows > 0) {
            $id = ''; 
        } else {
            break;
        }
    } while ($id === '');
    return $id;
}

$id = generateUniqueId($conn);
if ($id === false) {
    die("id处理异常");
}
$uname = "用户$id";
$imgurl = 'https://www.keaitupian.cn/cjpic/frombd/2/253/374108623/3369529135.jpg';
$sqlInsert = "INSERT INTO user (id, username, password, tximg, uname,spkcin,qddate,bdmail) VALUES ('$id', '$user_name', '" . $pass_word . "','$imgurl','$uname',0,'2024-01-01','$mail')";
if ($conn->query($sqlInsert) === TRUE) {
} else {
    die("注册失败");
}

$conn->close();
echo "<script>alert('账号注册成功'); window.location.href = '/center-person/lgreg.php';</script>";
exit();
?>